package com.itany.corejava.day12;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TestKeyEvent {

    private JFrame jf;
    private JLabel jl;
    private JTextArea jta;

    public TestKeyEvent() {
        jf=new JFrame();
        jl=new JLabel();
        jta=new JTextArea(20,40);
    }

    private void init(){
        JScrollPane jsp=new JScrollPane(jta);
        jf.add(jl, BorderLayout.NORTH);
        jf.add(jsp);
    }

    private void setStyle(){
        jl.setFont(new Font("宋体",Font.BOLD,26));
        jta.setFont(new Font("宋体",Font.BOLD,26));
        jta.setForeground(Color.RED);
    }


    private void addEventHandler(){
        jta.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                jl.setText(KeyEvent.getKeyText(e.getKeyCode())+"键被按下\n");
            }
        });
    }

    public void show(){
        init();
        setStyle();
        addEventHandler();
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestKeyEvent().show();
    }
}
